package com.huatai.saas.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.model.entity.ModelQuery;
import com.huatai.saas.service.UserModelAuthService;
import com.huatai.saas.vo.DelModelParamVo;
import com.huatai.saas.vo.ModelAuthParamVo;
import com.huatai.saas.vo.UserModelParamVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户模型授权表 前端控制器
 * </p>
 *
 * @author songxiude
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/modelAuth")
@Api(value = "用户模型授权", tags = "用户模型授权")
public class UserModelAuthController  {

	@Autowired
	private UserModelAuthService userModelAuthService;


	@PostMapping("/userModelAuth")
	@ApiOperation(value = "用户模型授权", notes = "用户模型授权")
	@ApiOperationSupport(order = 1, includeParameters = {
		"authParamVo.userId",
		"authParamVo.remark",
		"authParamVo.authStartTime",
		"authParamVo.authEndTime",
		"authParamVo.models"
	})
	public R<String> userModelAuth(@Valid @RequestBody ModelAuthParamVo authParamVo){
		if (userModelAuthService.userModelAuth(authParamVo)){
			return R.success("模型授权成功");
		}
		return R.fail("模型授权失败");
	}

	@PostMapping("/getUserModelList")
	@ApiOperation(value = "获取用户端模型列表", notes = "获取用户端模型列表")
	@ApiOperationSupport(order = 1, includeParameters = {
		"paramVo.modelName",
		"paramVo.userId",
		"paramVo.page",
		"paramVo.size"
	})
	public R getUserModelList(@RequestBody UserModelParamVo paramVo){
		return R.data(userModelAuthService.getUserModelList(paramVo));
	}

	@PostMapping("/delUserModels")
	@ApiOperation(value = "批量删除用户授权模型", notes = "批量删除用户授权模型")
	public R delUserModels(@Valid @RequestBody DelModelParamVo models){
		boolean delUserModels = userModelAuthService.delUserModels(models);
		if (!delUserModels){
			return R.fail("删除授权模型失败");
		}
		return R.success("删除授权模型成功");
	}

	@PostMapping("/getModelList")
	@ApiOperation(value = "获取模型列表", notes = "获取模型列表")
	public R getModelList(@RequestBody ModelQuery modelQuery){
		return R.data(userModelAuthService.getModelList(modelQuery));
	}

	@PostMapping("/getUserModels")
	@ApiOperation(value = "客户端获取模型列表(客户端)", notes = "客户端获取模型列表(客户端)")
	@ApiOperationSupport(order = 1, includeParameters = {
		"paramVo.modelName",
		"paramVo.industryKey",
		"paramVo.page",
		"paramVo.size"
	})
	public R getUserModels(@RequestBody UserModelParamVo paramVo){
		return R.data(userModelAuthService.getUserModels(paramVo));
	}

	@GetMapping("/getFileShardingNumber")
	@ApiOperation(value = "获取模型分片数(客户端)", notes = "获取模型分片数(客户端)")
	public R getFileShardingNumber(@ApiParam(name = "model_id",value = "模型ID",required = true) @RequestParam(name = "model_id") Long modelId){
		int shardingNumber = userModelAuthService.getFileShardingNumber(modelId);
		return R.data(shardingNumber);

	}

	@GetMapping("/modelDownload")
	@ApiOperation(value = "模型分片下载(客户端)", notes = "模型分片下载(客户端)")
	public R modelDownload(@ApiParam(name = "model_id",value = "模型ID",required = true) @RequestParam(name = "model_id") Long modelId,
						   @ApiParam(name = "sharding_num",value = "第几个分片",required = true) @RequestParam(name = "sharding_num") int shardingNum){
		userModelAuthService.modelDownload(modelId,shardingNum);
		return R.success("模型第"+shardingNum+"分片下载成功");
	}

	@GetMapping("/oneModelDownload")
	@ApiOperation(value = "单模型下载(客户端)", notes = "单模型下载(客户端)")
	public R oneModelDownload(@ApiParam(name = "model_id",value = "模型ID",required = true) @RequestParam(name = "model_id") Long modelId){
		userModelAuthService.oneModelDownload(modelId);
		return R.success("模型下载成功");
	}

}
